package com.example.waimaiandroid.updateInfo;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.example.waimai.context.UserInfoContext;
import com.example.waimaiandroid.R;


public class updateInfoMainFragment extends Fragment {

    private ImageView myImage;
    private TextView myName;
    private TextView myTel;
    private TextView myEmail;
    private TextView myPwd;
    private TextView del;

    private updateInfoMainCallBack callBack;

    public updateInfoMainFragment(updateInfoMainCallBack callBack) {
        this.callBack = callBack;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.update_info_main_fragment,container,false);
        initView(view);
        initListener();
        return view;
    }

    private void initView(View view){
        myImage = view.findViewById(R.id.myImage);
        myName = view.findViewById(R.id.myName);
        myTel = view.findViewById(R.id.myTelephone);
        myEmail = view.findViewById(R.id.myEmail);
        myPwd = view.findViewById(R.id.myPassword);
        del = view.findViewById(R.id.delInfo);

        // 设置数据
        Glide.with(view).load(UserInfoContext.mUser.getUserImage()).into(myImage);
        myName.setText(UserInfoContext.mUser.getUserName());
        myTel.setText(UserInfoContext.mUser.getTelephone());
        myEmail.setText(UserInfoContext.mUser.getEmail());
        myPwd.setText("*******");
    }

    // 设置监听器
    private void initListener(){
        myImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.updateImage(myImage);
            }
        });
        myName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.updateName(myName);
            }
        });
        myPwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.updatePwd();
            }
        });
        myEmail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.updateEmail(myEmail);
            }
        });
        del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.delInfo(v);
            }
        });
    }
}
